import { requestClient } from '#/api/request';
import { BaseModel } from '#/models/base';

export namespace InventoryApi {
  export interface Warehouse {
    warehouse_id: number;
    warehouse_name: string;
    warehouse_code: string;
    address?: string;
    manager_id?: number;
    contact_person?: string;
    contact_phone?: string;
    status: string;
    remark?: string;
    is_primary: boolean;
    capacity?: number;
    area?: number;
    description?: string;
    creator?: string;
    modifier?: string;
    is_deleted: boolean;
    create_time: string;
    update_time: string;
  }

  export interface Location {
    location_id: number;
    warehouse_id: number;
    location_name?: string;
    location_code: string;
    location_type?: string;
    status: string;
    remark?: string;
    description?: string;
    capacity?: number;
    current_load: number;
    is_aisle: boolean;
    zone?: string;
    row?: string;
    column?: string;
    level?: string;
    creator?: string;
    modifier?: string;
    is_deleted: boolean;
    create_time: string;
    update_time: string;
    warehouse?: Warehouse;
  }

  export interface Inventory {
    inventory_id: number;
    product_id: number;
    warehouse_id: number;
    location_id: number;
    quantity: number;
    available_quantity: number;
    reserved_quantity: number;
    unit_cost?: number;
    lot_number?: string;
    serial_number?: string;
    expiry_date?: string;
    manufactured_date?: string;
    remark?: string;
    creator?: string;
    modifier?: string;
    is_deleted: boolean;
    create_time: string;
    update_time: string;
    product?: any; // 产品信息
    warehouse?: Warehouse;
    location?: Location;
  }

  export interface InventoryTransaction {
    transaction_id: number;
    transaction_type: string;
    product_id: number;
    from_warehouse_id?: number;
    to_warehouse_id?: number;
    from_location_id?: number;
    to_location_id?: number;
    quantity: number;
    unit_cost?: number;
    total_cost?: number;
    reference_document?: string;
    reference_type?: string;
    lot_number?: string;
    serial_number?: string;
    remark?: string;
    creator?: string;
    modifier?: string;
    is_deleted: boolean;
    create_time: string;
    update_time: string;
    product?: any; // 产品信息
    from_warehouse?: Warehouse;
    to_warehouse?: Warehouse;
    from_location?: Location;
    to_location?: Location;
  }

  export interface InventoryCheck {
    check_id: number;
    check_code: string;
    check_date: string;
    status: string;
    warehouse_id: number;
    checker_id: number;
    approver_id?: number;
    approval_date?: string;
    start_time?: string;
    end_time?: string;
    total_variance_quantity: number;
    total_variance_amount: number;
    remark?: string;
    creator?: string;
    modifier?: string;
    is_deleted: boolean;
    create_time: string;
    update_time: string;
    warehouse?: Warehouse;
    details?: InventoryCheckDetail[];
  }

  export interface InventoryCheckDetail {
    detail_id: number;
    check_id: number;
    product_id: number;
    location_id: number;
    system_quantity: number;
    actual_quantity: number;
    variance_quantity: number;
    unit_cost?: number;
    variance_amount: number;
    lot_number?: string;
    serial_number?: string;
    remark?: string;
    creator?: string;
    modifier?: string;
    is_deleted: boolean;
    create_time: string;
    update_time: string;
    product?: any; // 产品信息
    warehouse?: Warehouse;
    location?: Location;
    check?: InventoryCheck;
  }
}

export class WarehouseModel extends BaseModel<InventoryApi.Warehouse> {
  /**
   * 仓库模型，用于与后端仓库API交互
   */
  constructor() {
    super('/inventory/warehouse/');
  }

  /**
   * 检查仓库编码是否唯一
   * @param code 仓库编码
   * @param id 当前仓库ID（编辑时使用，排除自身）
   * @returns 是否唯一
   */
  async checkCodeUnique(code: string, id?: number): Promise<boolean> {
    try {
      // 直接使用requestClient发送请求，避免依赖action方法
      const url = '/inventory/warehouse/check_code_unique/';
      const response = await requestClient.get(url, { params: { code, id } });
      // 确保返回boolean类型 - 正确处理响应数据
      return Boolean(response?.data ?? false);
    } catch (error) {
      console.error('检查仓库编码唯一性失败:', error);
      return false;
    }
  }
}

export class LocationModel extends BaseModel<InventoryApi.Location> {
  /**
   * 库位模型，用于与后端库位API交互
   */
  constructor() {
    super('/inventory/location/');
  }
}

export class InventoryModel extends BaseModel<InventoryApi.Inventory> {
  /**
   * 库存模型，用于与后端库存API交互
   */
  constructor() {
    super('/inventory/inventory/');
  }

  /**
   * 调整库存数量
   * @param params 调整参数
   */
  async adjust(params: {
    adjustment_quantity: number;
    adjustment_type: string;
    inventory_id: number;
    reason: string;
  }) {
    return this.action('adjust', params);
  }

  /**
   * 获取仓库列表（兼容组件中使用的方法名）
   * @param params 查询参数
   */
  async getList(params: any = {}) {
    return this.list(params);
  }

  /**
   * 获取库存汇总数据
   * @param params 查询参数，包含日期范围、仓库等筛选条件
   * @returns 库存汇总数据
   */
  async getSummary(params: any = {}) {
    return requestClient.get(
      '/api/admin/inventory/inventory/get_inventory_summary/',
      { params },
    );
  }

  /**
   * 移动库存
   * @param params 移动参数
   */
  async move(params: {
    inventory_id: number;
    move_quantity: number;
    reason: string;
    target_location_id: number | string;
    target_warehouse_id: number | string;
  }) {
    return this.action('move', params);
  }
}

export class InventoryTransactionModel extends BaseModel<InventoryApi.InventoryTransaction> {
  /**
   * 库存交易模型，用于与后端库存交易API交互
   */
  constructor() {
    super('/inventory/inventory_transaction/');
  }

  /**
   * 导出交易数据
   * @param params 导出参数
   */
  async exportData(params: any = {}) {
    return this.action('export', params);
  }

  /**
   * 获取交易列表（兼容组件中使用的方法名）
   * @param params 查询参数
   */
  async getList(params: any = {}) {
    return this.list(params);
  }
}

export class InventoryCheckModel extends BaseModel<InventoryApi.InventoryCheck> {
  /**
   * 库存盘点模型，用于与后端库存盘点API交互
   */
  constructor() {
    super('/inventory/inventory_check/');
  }

  /**
   * 创建盘点单
   * @param data 盘点单数据
   */
  async createCheck(data: any = {}) {
    return this.create(data);
  }

  /**
   * 导出盘点数据
   * @param params 导出参数
   */
  async exportData(params: any = {}) {
    return this.action('export', params);
  }

  /**
   * 获取盘点单详情
   * @param id 盘点单ID
   */
  /* async getDetail(id: number) {
    return this.detail(id);
  } */

  /**
   * 获取盘点列表（兼容组件中使用的方法名）
   * @param params 查询参数
   */
  async getList(params: any = {}) {
    return this.list(params);
  }

  /**
   * 更新盘点单
   * @param id 盘点单ID
   * @param data 盘点单数据
   */
  async updateCheck(id: number, data: any = {}) {
    return this.update(id, data);
  }
}

export class InventoryCheckDetailModel extends BaseModel<InventoryApi.InventoryCheckDetail> {
  /**
   * 库存盘点明细模型，用于与后端库存盘点明细API交互
   */
  constructor() {
    super('/inventory/inventory_check_detail/');
  }
}
